package com.hs.server.runnable;

import com.hs.server.config.MyConfig;
import com.hs.server.domain.ShResourcexx;
import com.hs.server.domain.VehQueuexxb;
import com.hs.server.mapper.XbjcMapper;
import com.hs.server.service.ShResourcexxService;
import com.hs.server.service.VehQueuexxbService;
import com.hs.server.service.impl.JianyanHsServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;

/**
 * @Description 调度模块
 * @Author: chenjiarui
 * @Date: 2021-07-14 16:16
 */
@RequiredArgsConstructor
@Slf4j
@Component
public class Dispatcher implements Runnable {
    @Autowired
    private VehQueuexxbService vehQueuexxbService;
    @Autowired
    private ShResourcexxService shResourcexxService;
    @Autowired
    private MyConfig myConfig;
    @Autowired
    private WorkProducer workProducer;
    @Autowired
    private WorkSearcher workSearcher;
    @Autowired
    private XbjcMapper xbjcMapper;
    private ExecutorService executorService;

    @PostConstruct
    public void init() {
        this.executorService = myConfig.executorService;
    }

    @SneakyThrows
    @Override
    public void run() {
        //重置队列表里中还没审核的数据状态为0
        vehQueuexxbService.updateShzt();
        //重置审核服务器为空闲状态
        shResourcexxService.updateSfkx();
        //重置veh_flow中yshbj=4的为0
        xbjcMapper.resetYshbj();
        //重置veh_photodes中ZPSHBJ=4的为0
        xbjcMapper.resetZPSHBJ();
        log.info("服务器资源重置成功！");

        //启动搜索作业线程 从新版数据库中获取审核数据并存入数据库
        executorService.execute(workSearcher);
        log.info("【启动作业搜索线程成功】");
        this.sleep(5000L);

        //启动作业生产者线程（将vehQueuexxb表数据放入队列workQueue中）
        executorService.execute(workProducer);
        log.info("【启动作业生产者线程成功】");
        this.sleep(5000L);

        //作业调度模块
        log.info("【启动作业消费者线程成功】");
        Scheduling();
    }

    private void Scheduling(){
        try {
            while (myConfig.openThread) {
                //查询是否有空闲的服务器资源
                ShResourcexx shResourcexx = shResourcexxService.getOneLeisureResourcexx();
                while (shResourcexx == null) {//暂无空闲服务器资源
                    this.sleep(50L);
                    shResourcexx = shResourcexxService.getOneLeisureResourcexx();
                }
                VehQueuexxb vehQueuexxb = null;
                //如果工作队列>0,就将队列推送到之前获取到的资源服务器上去审核
                if (myConfig.workQueue.size() > 0) {
                    try {
                        vehQueuexxb = myConfig.workQueue.take();//取，当队列为空时同步阻塞
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (vehQueuexxb != null) {
                    //初始化审核服务器
                    JianyanHsServiceImpl jianyanService = new JianyanHsServiceImpl(shResourcexx.getIpdz(),Integer.valueOf(shResourcexx.getDkh()));
                    //已经获取到服务器资源和作业，开始调用服务器执行审核
                    shResourcexx.setSfkx("0");//设置服务器资源为不空闲状态
                    shResourcexxService.updateResourcexx(shResourcexx);
                    //this.sleep(500L);
                    //====以上不存在多线程操作====
                    //执行审核模块
                    WorkCustomer workCustomer = new WorkCustomer(vehQueuexxb, shResourcexx,jianyanService);
                    executorService.execute(workCustomer);
                }
            }
        } catch (Exception e) {
            log.error("【调度线程出现了异常】", e);
        }
    }

    private void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
